const app = getApp()
var actbill = require('../../model/integral_luckdraw')
Page({
  mixins: [require('../../../../xc/mixins/basicMixin')],

  data: {
    // basicurl: app.globalData.basicurl,
    num: 1,
    lottery: [],
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    luckstatus: -1, //抽奖状态 -1没在抽奖 1抽奖中
    popshow: false,
    nothing:true,
    // rotatestatus:1
  },
  onpopClose(){
    this.setData({
      popshow: false,
    })
  },
  //判断可以抽奖否
  judge() {
    if (this.data.luckstatus == -1) {
      this.setData({
        luckstatus: 1
      })
    } else {
      return false;
    }

    if (!this.data.member.member_id) {
      this.login();
      this.setData({
        luckstatus: -1
      })
      return false;
    }
    var activity = this.data.activity;
    if (activity && activity.integral > 0) {
      var integral = activity.integral;
      if (parseInt(integral) <= parseInt(this.data.member.memberinfo.score)) {
        //抽奖
        this.luckdraw(activity.id)
        return;
      } else {
        this.error( this.data.integralname+"不足")
        this.setData({
          luckstatus: -1
        })
        return;
      }
    } else {
      this.error("配置错误")
      this.setData({
        luckstatus: -1
      })
      return;
    }

  },
  record() {
    var that = this;
    actbill.getrecord().then(res => {
    
      if (res['status'] == 1) {
        var obj = res['obj'];
        that.setData({
          recordlist: obj
        })
      }
    })
  },
  luckdraw(id) {
    var that = this;
    actbill.luckdraw(id).then(res => {
    
      if (res['status'] == 1) {
        var obj = res['obj'];
        that.setData({
          'activity.time': that.data.activity.time - 1,
          member: obj.userinfo
        })
        var index = obj.prize.name - 1;
        that._rotate(index);
      } else {
        var message = "操作错误";
        if (res['message']) {
          message = res['message']
        }
        wx.showModal({
          title: '错误',
          content: message,
        })
        this.setData({
          luckstatus: -1
        })
      }
    })
  },
  //抽奖
  rotate: function () {
    // if(this.data.rotatestatus==-1){
    //   return false
    // }
    // this.setData({
    //   rotatestatus:-1
    // })

    this.judge();
  },
  //——抽奖
  _rotate(index) {
    const self = this;
    let aniData = self.aniData;

    // var index = Math.round(Math.random() * (self.data.lottery.length - 1));
    console.log(index);
    aniData.rotate(3600 * self.data.num - 360 / (self.data.lottery.length) * index).step();
    self.setData({
      aniData: aniData.export(),
      num: self.data.num + 1
    })
    // var title = ""
    // var text = ""
    // if (self.data.lottery[index]['type'] == 'nothing') {
    //   title = "很遗憾"
    //   text = "什么也没抽中"
    // } else {
    //   title = '恭喜'
    //   text = "抽到了“" + self.data.lottery[index]['rewordname'] + "”"
    // }
    setTimeout(() => {
      this.showresult(self.data.lottery, index)
    }, 2100);

  },
  showresult(lottery, index) {
    if (lottery[index]['type'] == 'nothing') {
      this.setData({
        nothing:true
      })
    } else {
      this.setData({
        nothing:false,
        rewordname:lottery[index]['rewordname']
      })
      this.record()
    }
    this.setData({
      popshow:true,
      luckstatus: -1,
      // rotatestatus:1
    })
  },
  xcloaddata: function (options, config) {

    this.setData({
      basicurl:app.globalData.basicurl
    })
    var that = this;
    var update = {};
    var activityid = ""
    if (options.id) {
      activityid = options.id
    } else if (config.integral && config.integral.luckdraw) {
      update['luckdraw'] = config.integral.luckdraw
      activityid = config.integral.luckdraw.activityid
    }
    if (activityid) {
      this.getactivity(activityid)
      update['activityid'] = activityid
    } else {
      this.error("抽奖活动不存在，请后台配置", true)
    }
    this.setData(update)
    this.record()
  },
  getactivity(id) {
    var that = this;
    actbill.getactivity(id).then(res => {
     

      if (res['status'] == 1) {

        that.setData({
          activity: res['obj'],
          lottery: res['obj'].contents.editableTabs
        })
      }
    })
  },
  // error(message, back) {
  //   var homeurl = app.globalData.configdata.basic.homeurl
  //   wx.showModal({
  //     title: "错误",
  //     content: message,
  //     showCancel: false,
  //     success(res) {
  //       if (res.confirm) {
  //         console.log('用户点击确定')
  //         if (back) {
  //   console.log('wx.reLaunch8888888888888888')

  //           wx.reLaunch({
  //             url: homeurl,
  //           })
  //         }
  //       } else if (res.cancel) {
  //         console.log('用户点击取消')
  //       }
  //     }
  //   })
  // },
  afterlogin: function (options, config, userinfo) {
    var that = this;
    this.setData({
      member: userinfo
    })
    if (userinfo['member_id'] == 0) {
      this.login()
    }
  },
  // 登录注册
  login(e) {
    var that = this;
    app.xc_sys_config.authop.oppower("login").then(function (res) {
      that.setData({
        "member": app.globalData.member
      })
    })
  },
  onLoad: function () {
    const self = this;
    let aniData = wx.createAnimation({ //创建动画对象
      duration: 2000,
      timingFunction: 'ease'
    });
    self.aniData = aniData; //将动画对象赋值给this的aniData属性
  }
})